<template>
  <header class="header">
    <h1>todos</h1>
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      v-model="isAll"
    >
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model="taskName"
      @keyup.enter="addTask"
    /> <!-- 添加键盘弹起事件,enter键触发 -->

  </header>
</template>

<script>
export default {
  props: {
    arr: Array
  },
  data() {
    return {
      taskName: ''
    }
  },
  methods: {
    // 添加任务
    addTask() {
      // 子传父，带着任务名去找他爸
      this.$emit('addTask', this.taskName)
      // 添加完成之后清空input框
      this.taskName = ''
    }
  },
  computed: {
    // 实现全选框功能
    isAll: {
      set(checked) {
        this.arr.forEach((obj) => (obj.isDone = checked))
      },
      get(){
        return this.arr.every(obj => obj.isDone === true)
      }
    }
  }
}
</script>